<?php
/**
 * Created by PhpStorm.
 * User: huangwy
 * Date: 2018/2/1
 * Time: 16:28
 */
namespace Code\Controller;
use Think\Controller;
class OfficeController extends Controller{
    /**
     * word打开文档
     */
    public function word(){
        $word = new \COM("word.application") or die("Can't start Word!");
        $word->Documents->OPen("C:\Users\suncco\Desktop\su.doc");
        //$word->Quit();
    }

    /**
     * word转html，
     * @param $wordname
     * @param $htmlname
     */
    function word2html($wordname,$htmlname)
    {
        $word = new \COM("word.application") or die("Unable to instanciate Word");
        $word->Visible = 1;
        $word->Documents->Open($wordname);
        $word->Documents[1]->SaveAs($htmlname,8);
//        $word->Quit();
//        $word = null;
//        unset($word);
    }

    /**
     * word转html
     */
    function open(){
        $this->word2html('C:/Users/suncco/Desktop/readDoc.doc','C:/Users/suncco/Desktop/readDoc.pdf');
    }

    /**
     * 读取excel文件
     */
    public function readExcel(){
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('xls', 'xlsx');// 设置附件上传类
            $upload->savePath  =      '/'; // 设置附件上传目录
            // 上传文件
            $info   =   $upload->upload();
            $filename = './Uploads'.$info['photo']['savepath'].$info['photo']['savename'];
            $exts = $info['ext'];
            //print_r($info);exit;
            if(!$info) {// 上传错误提示错误信息
                $this->ajaxReturn(array('statusCode'=>300, 'message' => $upload->getError()));
            }else{// 上传成功
                $result = $this->excel_import($filename, $exts);
            }
        }else{
            $this->display();
        }
    }

    protected function excel_import($filename, $exts = 'xls') {
        import("Org.Util.PHPExcel");   // 这里不能漏掉
        import("Org.Util.PHPExcel.IOFactory");
        if($exts == 'xls'){
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        }else{
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }
        $objPHPExcel = $objReader->load($filename,$encode='utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数


        for ($currentRow = 1; $currentRow <= $highestRow; $currentRow++) {
            //从哪列开始，A表示第一列
            for ($currentColumn = 'A'; $currentColumn <= $highestColumn; $currentColumn++) {
                //数据坐标
                $address = $currentColumn . $currentRow;
                //读取到的数据，保存到数组$arr中
// 	            $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
                $value = $objPHPExcel->getActiveSheet()->getCell($address)->getValue();
//                if(empty($value)){
//                    continue;
//                }
                $data[$currentRow][$currentColumn] = $value;
            }
        }

        var_dump($data);die;

    }

}